package com.jic.point.controller;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.PageInfo;

import java.util.*;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import com.jic.point.model.vo.request.ConfActivityRuleDetailTemplateRequest;
import com.jic.point.model.vo.response.ConfActivityRuleDetailTemplateResponse;
import com.jic.point.model.entity.ConfActivityRuleDetailTemplate;
import com.jic.point.service.ConfActivityRuleDetailTemplateService;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.l;

/**
 * @Author: admin
 * @date: 2020-10-29 17:02:01
 */
 @RestController
 @Slf4j
 @RequestMapping("/confActivityRuleDetailTemplate")
 @Api(tags = " [积分规则模板表]控制类-PC")
public class ConfActivityRuleDetailTemplateController {
	@Autowired
	private ConfActivityRuleDetailTemplateService service;
	
	
     /**
      *新增记录
      * @param request 新增[积分规则模板表]请求参数
      * @return ConfActivityRuleDetailTemplateResponse 新增[积分规则模板表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[积分规则模板表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfActivityRuleDetailTemplateRequest request) throws Throwable {
     		ConfActivityRuleDetailTemplate confActivityRuleDetailTemplate = new ConfActivityRuleDetailTemplate();
    		BeanUtils.copyProperties(request, confActivityRuleDetailTemplate);
    	    long flag = service.save(confActivityRuleDetailTemplate);
     return RestResult.success(flag);
    }
	
     /**
      * 删除记录
      * @param request 删除[积分规则模板表]请求参数
      * @return ConfActivityRuleDetailTemplateResponse 删除[积分规则模板表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[积分规则模板表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody ConfActivityRuleDetailTemplateRequest request) throws Throwable {
      		ConfActivityRuleDetailTemplate confActivityRuleDetailTemplate = new ConfActivityRuleDetailTemplate();
	     	BeanUtils.copyProperties(request, confActivityRuleDetailTemplate);
	         long flag = service.deleteByPrimaryKeyLogically(confActivityRuleDetailTemplate);
      return RestResult.success(flag);
     }

    /**
     * 新增或修改记录
     * @param request 新增或修改[积分规则模板表]请求参数
     * @return ConfActivityRuleDetailTemplateResponse 新增或修改[积分规则模板表]响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[积分规则模板表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfActivityRuleDetailTemplateRequest request) throws Throwable {
        ConfActivityRuleDetailTemplate confActivityRuleDetailTemplate = new ConfActivityRuleDetailTemplate();
        BeanUtils.copyProperties(request, confActivityRuleDetailTemplate);
        long flag =   service.saveOrUpdate(confActivityRuleDetailTemplate);
        return RestResult.success(flag);
    }

     /**
      * 修改记录
      * @param request 修改[积分规则模板表]请求参数
      * @return ConfActivityRuleDetailTemplateResponse 修改[积分规则模板表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[积分规则模板表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody ConfActivityRuleDetailTemplateRequest request) throws Throwable {
      		ConfActivityRuleDetailTemplate confActivityRuleDetailTemplate = new ConfActivityRuleDetailTemplate();
	     	BeanUtils.copyProperties(request, confActivityRuleDetailTemplate);
	         long flag = service.updateByPrimaryKeySelective(confActivityRuleDetailTemplate);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[积分规则模板表]详情请求参数
      * @return ConfActivityRuleDetailTemplateResponse 查询[积分规则模板表]详情响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[积分规则模板表]详情")
      @ResponseBody
     public RestResult<ConfActivityRuleDetailTemplateResponse> getByPrimaryKey(@Validated  @RequestBody ConfActivityRuleDetailTemplateRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	ConfActivityRuleDetailTemplate confActivityRuleDetailTemplate = new ConfActivityRuleDetailTemplate();
	     	BeanUtils.copyProperties(request, confActivityRuleDetailTemplate);
	     	ConfActivityRuleDetailTemplate result = service.getByPrimaryKey(confActivityRuleDetailTemplate);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     ConfActivityRuleDetailTemplateResponse bo = new ConfActivityRuleDetailTemplateResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new ConfActivityRuleDetailTemplateResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfActivityRuleDetailTemplate>> listPage (@RequestBody ConfActivityRuleDetailTemplateRequest request) throws Throwable {
      ConfActivityRuleDetailTemplate param = new ConfActivityRuleDetailTemplate();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *CONF_ACTIVITY_RULE_DETAIL_TEMPLATE 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [积分规则模板表] ")
      @ResponseBody
      public RestResult<List<ConfActivityRuleDetailTemplateResponse>>  listConfActivityRuleDetailTemplateAllRecord(@RequestBody ConfActivityRuleDetailTemplateRequest request) throws Throwable {
      		ConfActivityRuleDetailTemplate param = new ConfActivityRuleDetailTemplate();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<ConfActivityRuleDetailTemplate> list = service.listAllRecord(param);
			List<ConfActivityRuleDetailTemplateResponse> responseList = list.stream().map(temp -> {
			ConfActivityRuleDetailTemplateResponse bo = new ConfActivityRuleDetailTemplateResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }

    /**
     *CONF_ACTIVITY_RULE_DETAIL_TEMPLATE 查询列表（不分页）
     * @param  request 前端请求参数
     */
    @PostMapping("/getByPrimaryKeyScene")
    @ApiOperation(value = "分组查询现有场景 [积分规则模板表] ")
    @ResponseBody
    public RestResult<List<ConfActivityRuleDetailTemplateResponse>>  getByPrimaryKeyScene(@RequestBody ConfActivityRuleDetailTemplateRequest request) throws Throwable {

        List<ConfActivityRuleDetailTemplate> list = service.getByPrimaryKeySence();
        List<ConfActivityRuleDetailTemplateResponse> responseList = list.stream().map(temp -> {
            ConfActivityRuleDetailTemplateResponse bo = new ConfActivityRuleDetailTemplateResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     *
     * @return
     */
    @PostMapping("/getBySceneIdTemplate")
    @ApiOperation(value = "根据sceneid查询模板 [积分规则模板表] ")
    @ResponseBody
    public RestResult<List<Map>> selBySceneIdTemplate(@RequestBody Map map){
        String[] s =  map.get("sceneId").toString().split(",");
        ConfActivityRuleDetailTemplate confActivityRuleDetailTemplate = new ConfActivityRuleDetailTemplate();
        List<Map> arrayList = new ArrayList();

        Arrays.asList(s).stream().forEach(sceneId -> {
            Map m = new HashMap();
            confActivityRuleDetailTemplate.setSceneId(Long.parseLong(sceneId));
            List<Map> maps = service.selBySceneIdTemplate(confActivityRuleDetailTemplate);
            m.put("sceneId",maps.get(0).get("sceneId"));
            m.put("sceneName",maps.get(0).get("sceneName"));
            m.put("id",maps.get(0).get("id"));
            m.put("list",maps);
            arrayList.add(m);
        });

        return RestResult.success(arrayList);
    }
}

	
